package community.hospital.service.domain.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import community.hospital.entity.Schedule;
import community.hospital.entity.request.ListScheduleRequest;
import community.hospital.mapper.ScheduleMapper;
import community.hospital.service.domain.ScheduleService;
import jdk.nashorn.internal.runtime.regexp.joni.Warnings;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;

/**
 * @author yindingjie
 */
@Service
@SuppressWarnings("all")
public class ScheduleServiceImpl extends ServiceImpl<ScheduleMapper, Schedule> implements ScheduleService {
    @Override
    public Page<Schedule> listSchedule(ListScheduleRequest request) {
        Date detailTime = request.getDetailTime();
        LocalDateTime dateTime = LocalDateTimeUtil.of(detailTime);
        Integer year = dateTime==null?null:dateTime.getYear();
        Integer month = dateTime==null?null:dateTime.getMonthValue();
        Integer day = dateTime==null?null:dateTime.getDayOfMonth();
        Long hospitalId = request.getHospitalId();
        Long departmentId = request.getDepartmentId();
//        String name = request.getName();
        Page<Schedule> schedulePage = new Page<>(request.getPageNumber(), request.getPageSize());
        lambdaQuery().eq(year != null, Schedule::getYear, year)
                .eq(hospitalId!=null,Schedule::getHospitalId,hospitalId)
                .eq(departmentId!=null,Schedule::getDepartment1Id,departmentId)
                .or(departmentId!=null,t->t.eq(Schedule::getDepartment2Id,departmentId))
                .eq(month != null, Schedule::getMonth, month)
                .eq(day != null, Schedule::getDay, day)
                .eq(request.getState() != null, Schedule::getState, request.getState())
                .eq(request.getDoctorId()!=null,Schedule::getDoctorId,request.getDoctorId())
                .page(schedulePage);
        return schedulePage;
    }

    @Override
    public int countByDoctorIdAndTime(int year,int month,int day, Long doctorId) {
        return lambdaQuery()
                .eq(Schedule::getYear, year)
                .eq(Schedule::getMonth, month)
                .eq(Schedule::getDay, day)
                .eq(Schedule::getDoctorId, doctorId)
                .count()
                .intValue();
    }

    @Override
    public Schedule querySchedule(Long doctorId, LocalDate localDate) {
        Schedule schedule = lambdaQuery()
                .eq(Schedule::getDoctorId, doctorId)
                .eq(Schedule::getYear, localDate.getYear())
                .eq(Schedule::getMonth, localDate.getMonth().getValue())
                .eq(Schedule::getDay, localDate.getDayOfMonth())
                .one();
        return schedule;
    }




}
